package com.ygqh.baby.service.wechat.model.good;

import com.alibaba.fastjson.JSONObject;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.ProductStatus;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductImages;
import com.ygqh.baby.po.YgSku;
import com.ygqh.baby.template.AbstractSplitListTemplate;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: sunshuo
 * @Date: 2019/5/24 16:26
 * @Version: 1.0
 */
public class GoodProductBuilder {

	private YgProduct productInfo;

	private GoodProduct product;

	public static GoodProductBuilder builder(YgProduct productInfo) {
		GoodProductBuilder builder = new GoodProductBuilder();
		builder.productInfo = productInfo;
		builder.product = new GoodProduct();
		return builder;
	}

	public GoodProductBuilder build() {
		buildProductInfo();
		buildSkuInfo();
		return this;
	}

	private void buildSkuInfo() {
		List<GoodSku> skuList = new ArrayList<>();
		productInfo.getYgSkus().forEach(skuInfo -> {
			GoodSku sku = new GoodSku();
			sku.setSkuId(skuInfo.getId().toString());
			BigDecimal price;
			if (skuInfo.getPromotionPrice() != null && skuInfo.getPromotionPrice().compareTo(BigDecimal.ZERO) > 0) {
				price = skuInfo.getPromotionPrice();
			} else {
				price = skuInfo.getSalesPrice();
			}
			sku.setPrice(price.multiply(BigDecimal.valueOf(100)).intValue());
			sku.setOriginalPrice(skuInfo.getSourceSalesPrice().multiply(BigDecimal.valueOf(100)).intValue());
			sku.setStatus(buildSkuStatus(skuInfo));
			sku.setSkuAttrList(Collections.singletonList(new GoodProductAttr("尺码", skuInfo.getSizeName())));
			skuList.add(sku);
		});
		product.setSkuList(skuList);
	}

	private int buildSkuStatus(YgSku skuInfo) {
		if (DataStatus.Delete.equals(skuInfo.getStatus())
				|| !ProductStatus.OnSale.equals(productInfo.getSaleStatus())) {
			return GoodSku.SKU_IN_STOCK;
		}
		if (skuInfo.getStock().getAmount() <= 0) {
			return GoodSku.SKU_OUT_STOCK;
		}
		return GoodSku.SKU_ON_SALE;
	}

	private void buildProductInfo() {
		product.setItemCode(productInfo.getId().toString());
		product.setTitle(productInfo.getProductName());
		product.setDesc(StringUtils.isBlank(productInfo.getSellingPoint()) ?
				productInfo.getProductName() : productInfo.getSellingPoint());
		product.setCategoryList(getCategoryNameList());
		product.setImageList(getImageList());
		product.setSrcWxappPath("pages/goods/detail/detail?sourceCode=haowuquan&id=" + productInfo.getId());
		product.setAttrList(Collections.singletonList(new GoodProductAttr("颜色", productInfo.getShortName())));
		product.setCanBeSearch(true);
	}

	public GoodProduct get() {
		return product;
	}

	private List<String> getImageList() {
		LinkedList<String> imageList = new LinkedList<>();
		for (YgProductImages images : productInfo.getProductImages()) {
			int index = images.getImagePath().lastIndexOf(".");
			String url = images.getImagePath().substring(0, index) + "-800*800"
					+ images.getImagePath().substring(index);
			if (images.getIsMain()) {
				imageList.addFirst(url);
			} else {
				imageList.add(url);
			}
		}
		return imageList;
	}

	private List<String> getCategoryNameList() {
		List<String> categoryNameList = new ArrayList<>();
		if (StringUtils.isNotBlank(productInfo.getCategoryNames())) {
			for (String categoryName : productInfo.getCategoryNames().split(",")) {
				if (StringUtils.isNotBlank(categoryName)) {
					categoryNameList.add(categoryName);
				}
			}
		}
		if (categoryNameList.size() == 0) {
			categoryNameList.add("其它");
		}
		return categoryNameList;
	}

	public List<GoodProduct> splitSku(int size) {
		if (product.getSkuList().size() <= size) {
			return Collections.singletonList(product);
		}
		// TODO 只推前几个SKU，不影响搜索商品
		List<GoodSku> recordList = new ArrayList<>(size);
		for (GoodSku sku : product.getSkuList()) {
			if (recordList.size() == size) {
				break;
			}
			recordList.add(sku);
		}
		product.setSkuList(recordList);
		return Collections.singletonList(product);
		/*List<GoodProduct> productList = new ArrayList<>();
		new AbstractSplitListTemplate<GoodSku>(product.getSkuList(), size) {
			@Override
			public int execute(List<GoodSku> recordList) {
				GoodProduct p = new GoodProduct();
				BeanUtils.copyProperties(product, p);
				p.setSkuList(new ArrayList<>(recordList));
				productList.add(p);
				return 0;
			}
		}.split();
		return productList;*/
	}

	public static void main(String[] args) {
		GoodProduct product = new GoodProduct();
		product.setItemCode("000000");
		List<GoodSku> skuList = new ArrayList<>();
		GoodSku s1 = new GoodSku();
		s1.setSkuId("1111");
		GoodSku s2 = new GoodSku();
		s2.setSkuId("2222");
		GoodSku s3 = new GoodSku();
		s3.setSkuId("3333");
		GoodSku s4 = new GoodSku();
		s4.setSkuId("4444");
		skuList.add(s1);
		skuList.add(s2);
		skuList.add(s3);
		skuList.add(s4);
		product.setSkuList(skuList);
		List<GoodProduct> productList = new ArrayList<>();
		new AbstractSplitListTemplate<GoodSku>(product.getSkuList(), 2) {
			@Override
			public int execute(List<GoodSku> recordList) {
				GoodProduct p = new GoodProduct();
				BeanUtils.copyProperties(product, p);
				p.setSkuList(new ArrayList<>(recordList));
				productList.add(p);
				return 0;
			}
		}.split();
		JSONObject object = new JSONObject();
		object.put("product_list", productList);
		System.out.println(object.toJSONString());
	}
}
